<?php

use \EasyWeChat\Factory;

//获取微信accessToken
function getAccessToken() {

    $redis = new \Predis\Client(config("constants.redis_config"));
    $accessToken = $redis->get("xdAccessToken");

    if(isNullOrEmpty($accessToken)) {

        $appid = config("constants.miniProgramConfig.appid");
        $appSecret = config("constants.miniProgramConfig.app_secret");

        $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='
            .$appid.'&secret='.$appSecret;
        $html = file_get_contents($url);
        $output = json_decode($html, true);
        $accessToken = $output['access_token'];

        $redis->set("xdAccessToken", $accessToken, 3000);

    }

    return $accessToken;
}

function wechatPost($url,$post_data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //没有这个会自动输出，不用print_r();也会在后面多个1
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $output = curl_exec($ch);
    curl_close($ch);
    $out = json_decode($output);
    return $out;
}

function wechatCurlPost($url,$post_data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // post数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
    $output = curl_exec($ch);
    curl_close($ch);
    //返回获得的数据
    return $output;
}

/**
 * 获取小程序码
 * @param $scene
 * @param $page
 * @param int $width
 * @return mixed
 */
function getWxaCode($scene, $page, $width=100) {

    $accessToken = getAccessToken();

    $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$accessToken";

    $result = wechatCurlPost($url, [
        'scene' => $scene,
        'path' => $page,
        'width' => $width,
        'auto_color' => false,
        'line_color' => [
            'r' => 0,
            'g' => 0,
            'b' => 0
        ]

    ]);

    return $result;

}

/**
 * 商品收益到账通知
 * @param $data
 * @return mixed
 */
function sendGoodsProfitNotify($data) {

    $accessToken = getAccessToken();

    $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$accessToken";

    wechatCurlPost($url, [
        'touser' => $data["openid"],
        'template_id' => "dwgP16jOmfZE_oJL-LqjNdJTd5AT2mOBicAN-qtEFtc",
        'page' => $data["page"],
        'form_id' => $data["form_id"],
        'data' => [
            'keyword1' => [
                'value' => $data['profit_price']
            ],
            'keyword2' => [
                'value' => $data['customer_name']
            ],
            'keyword3' => [
                'value' => $data['goods_name']
            ],
            'keyword4' => [
                'value' => $data['order_time']
            ],
            'keyword5' => [
                'value' => $data['order_no']
            ],
            'keyword6' => [
                'value' => $data['tip'],
                'color' => "#d70303"
            ],
        ]
    ]);

}

/**
 * 邀请收益到账通知
 * @param $data
 * @return mixed
 */
function sendRecommendProfitNotify($data) {

    $accessToken = getAccessToken();

    $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$accessToken";

    wechatCurlPost($url, [
        'touser' => $data["openid"],
        'template_id' => "dwgP16jOmfZE_oJL-LqjNeBdbiGGU_Q5sk0vYXxtMfI",
        'page' => $data["page"],
        'form_id' => $data["form_id"],
        'data' => [
            'keyword1' => [
                'value' => $data['profit_price']
            ],
            'keyword2' => [
                'value' => $data['recommend_customer_name']
            ],
            'keyword3' => [
                'value' => $data['goods_name']
            ],
            'keyword4' => [
                'value' => $data['account_time']
            ],
            'keyword5' => [
                'value' => $data['tip'],
                'color' => "#d70303"
            ]
        ]
    ]);

}

/**
 * 交易提醒模板
 * @param $data
 * @return mixed
 */
function sendTransactionReminding($data) {

    $accessToken = getAccessToken();

    $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$accessToken";

    wechatCurlPost($url, [
        'touser' => $data["openid"],
        'template_id' => "h3VfreWMDDR5Sx8rZG3PEWNQXt361dok1FJLWg69yec",
        'page' => $data["page"],
        'form_id' => $data["form_id"],
        'data' => [
            'keyword1' => [
                'value' => $data['goods_name']
            ],
            'keyword2' => [
                'value' => $data['transaction_time']
            ],
            'keyword3' => [
                'value' => $data['pay_amt']
            ],
            'keyword4' => [
                'value' => $data['tip'],
                'color' => "#d70303"
            ]
        ]
    ]);

}